<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Adm_login_siswa extends CI_Controller
{
    public function index()
    {
        $data['rslt_data_login'] = $this->db->select('data_siswa.*, user_login.*')->from('user_login')->join('data_siswa', 'data_siswa.id = user_login.kode_siswa', 'INNER')->order_by('user_login.id', 'ASC')->get();
        $data['r_siswa'] = $this->db->query('SELECT g.id, g.nama_siswa, g.nomor_induk FROM data_siswa g LEFT JOIN user_login u ON(g.id=u.kode_siswa) WHERE u.id IS NULL AND g.lulus=0 ORDER BY g.nama_siswa ASC');
        $this->template->admin('admin/v_adm_login_siswa', $data, 2, "Data Login User Siswa");
    }
    
    public function edit($id = null)
    {
        if($id === null)
        {
            show_404();
        }
        else
        {
            $rslt_data = $this->db->select('*')->from('user_login')->where('id', $id)->limit(1)->order_by('id', 'ASC')->get();
            if($rslt_data->num_rows() == 1)
            {
                $data['r_guru'] = $this->db->query('SELECT g.id, g.nama_siswa FROM data_siswa g LEFT JOIN user_login u ON(g.id=u.kode_siswa) WHERE g.lulus=0 AND (u.id IS NULL OR u.id="'.$id.'") ORDER BY g.nama_siswa ASC');
                $rslt_data = $rslt_data->row();
                $data['id'] = $rslt_data->id;
                $data['username'] = $rslt_data->username;
                $data['password'] = $rslt_data->password;
                $data['id_siswa'] = $rslt_data->kode_siswa;
                $this->template->admin('admin/v_adm_login_ubah_siswa', $data, 2, "Form Ubah Data Login User Siswa");
            }
            else
            {
                show_404();
            }
        }
    }
    
    public function hapus($id = null)
    {
        if($id === null)
        {
            show_404();
        }
        else
        {
            $this->db->where('id', $id)->limit(1)->delete('user_login');
            redirect('admin/adm_login_siswa');
        }
    }
    
    public function simpan_data($proses = null)
    {
        if($proses == null)
        {
            show_404();
        }
        else
        {
            $username = $this->input->post('username');
            $id_siswa = $this->input->post('id_siswa');
            $password = $this->input->post('password');
            if($proses == 'tambah')
            {
                    $this->db->insert('user_login', 
                            array('username' => $username,
                                'password' => $password,
                                'stat' => 2,
                                'kode_siswa' => $id_siswa)
                            );
            }
            else if($proses == 'ubah')
            {
                $id = $this->input->post('id');
                if($id != '')
                {
                    $this->db->set( 
                            array('username' => $username,
                                'password' => $password,
                                'kode_siswa' => $id_siswa)
                            )
                        ->where('id', $id)
                        ->update('user_login');
                }
            }
            redirect('admin/adm_login_siswa');
        }
    }
}

/* End of file adm_login_siswa.php */
/* Location: ./application/controllers/admin/adm_login_siswa.php */